package com.butterfly.server.controller;

import cn.hutool.core.bean.BeanUtil;
import com.butterfly.common.model.SoaResponse;
import com.butterfly.common.utils.SoaResponseUtil;
import com.butterfly.server.request.user.LoginRequest;
import com.butterfly.server.response.WxUserDetailInfoResp;
import com.butterfly.wx.model.WeChatModel;
import com.butterfly.wx.model.WeChatSessionModel;
import com.butterfly.wx.service.WxApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/user")

public class UserController {

    @Autowired
    private WxApi wxApi;

    /**
     * 微信登录
     */
    @PostMapping("/login")
    public SoaResponse<WxUserDetailInfoResp> userLogin(@RequestBody LoginRequest request) {
        WeChatSessionModel weChatModel = wxApi.getWeChatModel(request.getCode());
        if (!Objects.equals(weChatModel.getErrcode(), "0")) {
            return SoaResponseUtil.failure(null, weChatModel.getErrcode(), weChatModel.getErrmsg());
        }
        WxUserDetailInfoResp wxUserDetailInfoResp = new WxUserDetailInfoResp();
        return SoaResponseUtil.success(wxUserDetailInfoResp);
    }
}
